package lpl.com.xyd.test.manage.factory;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.factory.AbstractDomainFactory;
import lpl.com.xyd.test.manage.common.factory.AutowiredDomainFactory;
import lpl.com.xyd.test.manage.dao.WorkPlanMapperDAO;
import lpl.com.xyd.test.manage.domain.WorkPlanDomain;
import lpl.com.xyd.test.manage.dto.WorkPlanDTO;
import lpl.com.xyd.test.manage.param.WorkPlanParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WorkPlanDomainFactory extends AbstractDomainFactory<WorkPlanDomain, WorkPlanParam> {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkPlanDomainFactory.class);

    @Autowired
    private AutowiredDomainFactory autowiredDomainFactory;

    @Autowired
    private WorkPlanMapperDAO workPlanMapperDAO;

    @Override
    public WorkPlanDomain create(WorkPlanParam param) {
        LOGGER.info("创建领域，domain:{}", JSONObject.toJSONString(param));
        WorkPlanDomain domain = autowiredDomainFactory.create(WorkPlanDomain.class);
        BeanUtils.copyProperties(param,domain);
        return domain;
    }

    @Override
    public WorkPlanDomain active(String domainId) {
        return null;
    }

    @Override
    public List<WorkPlanDomain> activeList(WorkPlanParam param) {
        List<WorkPlanDTO> workPlanDTOS =  workPlanMapperDAO.selectByParam(param);
        if (null == workPlanDTOS || workPlanDTOS.size() <= 0){
            LOGGER.info("没有数据");
            return null;
        }
        List<WorkPlanDomain> domains = new ArrayList<>(workPlanDTOS.size());
        workPlanDTOS.forEach(v->{
            WorkPlanDomain domain = new WorkPlanDomain();
            BeanUtils.copyProperties(v,domain);
            domains.add(domain);
        });
        return domains;
    }

    @Override
    public Boolean store(WorkPlanDomain domain) {
        LOGGER.info("保存领域，domain:{}", JSONObject.toJSONString(domain));
        WorkPlanDTO workPlanDTO = new WorkPlanDTO();
        BeanUtils.copyProperties(domain,workPlanDTO);
        int result = workPlanMapperDAO.insert(workPlanDTO);
        return result > 0;
    }

    @Override
    public Boolean reStore(WorkPlanDomain domain) {
        return null;
    }

    @Override
    public Boolean reStore(List<WorkPlanDomain> domains) {
        return null;
    }

    @Override
    public boolean delete(Long domainId) {
        return false;
    }

    @Override
    public boolean delete(List<Long> domainId) {
        return false;
    }
}
